﻿using Evision.Insure;
using Evision.Zlsoft.Miaiap.Core.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Zlsy.Core.Data;

namespace Evision.Zlsoft.Miaiap.Core.Service {
   
    public class ModelPublic {
        private Ado _ado;
        public ModelPublic() {
            _ado = Ado.Default;
        }
        public static InputPublic GetInputPublic() {
            string sql = @"Select p.Id 人员id, p.姓名 操作员名称,p.编号 操作员代码,p.执业类别 人员类别 ,p.简码, p.编号, d.名称 As 科室名称,d.编码 科室代码, m.部门id, u.用户名,d.id 科室id
                        from 上机人员表 U, 人员表 P, 部门表 D, 部门人员 M 
                        Where u.人员id = p.Id And p.Id = m.人员id And m.缺省 = 1 And m.部门id = d.Id And u.用户名 = User";
            DataRow dr = Adodb.QueryForRow(sql);
            // 创建 BaseXml 对象
            InputPublic baseXml = new InputPublic {
                source = "HIS",
                departmentCode = dr["科室id"].ToString(),
                departmentName = dr["科室名称"].ToString(),
                doctor = new Doctor {
                    code = dr["人员id"].ToString(),
                    name = dr["操作员名称"].ToString(),
                    type = dr["人员类别"].ToString(),
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(InputPublic));
            using (StringWriter writer = new StringWriter()) {
                serializer.Serialize(writer, baseXml);
                string xmlString = writer.ToString();
                Console.WriteLine(xmlString);
            }
            return baseXml;
        }
        public static InputPublic GetInputPublic(InputPrescriptionApproval inputPrescriptionApproval) {
            string sql = @"Select p.Id 人员id, p.姓名 操作员名称,p.编号 操作员代码,p.执业类别 人员类别 ,p.简码, p.编号, d.名称 As 科室名称,d.编码 科室代码, m.部门id, u.用户名,d.id 科室id
                        from 上机人员表 U, 人员表 P, 部门表 D, 部门人员 M 
                        Where u.人员id = p.Id And p.Id = m.人员id And m.缺省 = 1 And m.部门id = d.Id And u.用户名 = User";
            DataRow dr = Adodb.QueryForRow(sql);
            InputPublic baseXml = null;

            if (inputPrescriptionApproval?.prescriptions?.Count > 0) {
                baseXml = new InputPublic {
                    source = "HIS",
                    departmentCode = inputPrescriptionApproval.prescriptions[0].departmentCode,
                    departmentName = inputPrescriptionApproval.prescriptions[0].departmentName,
                    doctor = new Doctor {
                        code = dr["人员id"]?.ToString() ?? string.Empty,
                        name = dr["操作员名称"]?.ToString() ?? string.Empty,
                        type = dr["人员类别"]?.ToString() ?? string.Empty,
                    }
                };
            }


            XmlSerializer serializer = new XmlSerializer(typeof(InputPublic));
            using (StringWriter writer = new StringWriter()) {
                serializer.Serialize(writer, baseXml);
                string xmlString = writer.ToString();
                Console.WriteLine(xmlString);
            }
            return baseXml;
        }
    }
}
